#include "gui.hh"

Gui::Gui(): m_box1{Gtk::Orientation::ORIENTATION_HORIZONTAL}, m_box2{Gtk::Orientation::ORIENTATION_HORIZONTAL}, m_box3{Gtk::Orientation::ORIENTATION_HORIZONTAL}, m_box4{Gtk::Orientation::ORIENTATION_HORIZONTAL} {

  gui2 = 0;
  guiAbout = 0; 
  
  set_hierarchy();
  draw_widgets();
  show_all_children(); 
}

bool Gui::is_number(const std::string& s){
  std::string::const_iterator it = s.begin();
  while(it != s.end() && std::isdigit(*it))
    ++it;
  return !s.empty() && it == s.end(); 
}

void Gui::printClose(){
  gui2 = 0; 
}

void Gui::print(std::string str){ 
  if(gui2 != 0)
    return; 
  
  gui2 = new Gui2(str);
  gui2->signal_hide().connect(sigc::mem_fun(*this, &Gui::printClose));
  gui2->show(); 
  
}

void Gui::aboutClose(){
	guiAbout = 0; 
}

void Gui::about(){
	if(guiAbout != 0)
		return;
		
	guiAbout = new GuiAbout;
	guiAbout->signal_hide().connect(sigc::mem_fun(*this, &Gui::aboutClose));
	guiAbout->show();  
}

void Gui::on_button1(){
  float a[3][3], b[3];

  if(is_number(m_entryX1.get_text()) &&
     is_number(m_entryY1.get_text()) &&
     is_number(m_entryZ1.get_text()) &&
     is_number(m_entryX2.get_text()) &&
     is_number(m_entryY2.get_text()) &&
     is_number(m_entryZ2.get_text()) &&
     is_number(m_entryX3.get_text()) &&
     is_number(m_entryY3.get_text()) &&
     is_number(m_entryZ3.get_text()) &&
     is_number(m_entryB1.get_text()) &&
     is_number(m_entryB2.get_text()) &&
     is_number(m_entryB3.get_text()) 
     ){
    a[0][0] = std::stof(m_entryX1.get_text());
    if(m_comboboxX1.get_active_text() == "-")
      a[0][0] *= -1;

    a[0][1] = std::stof(m_entryY1.get_text());
    if(m_comboboxY1.get_active_text() == "-")
      a[0][1] *= -1;

    a[0][2] = std::stof(m_entryZ1.get_text());
    if(m_comboboxZ1.get_active_text() == "-")
      a[0][2] *= -1;
  
    a[1][0] = std::stof(m_entryX2.get_text());
    if(m_comboboxX2.get_active_text() == "-")
      a[1][0] *= -1;
  
    a[1][1] = std::stof(m_entryY2.get_text());
    if(m_comboboxY2.get_active_text() == "-")
      a[1][1] *= -1;

    a[1][2] = std::stof(m_entryZ2.get_text());
    if(m_comboboxZ2.get_active_text() == "-")
      a[1][2] *= -1; 
  
    a[2][0] = std::stof(m_entryX3.get_text());
    if(m_comboboxX3.get_active_text() == "-")
      a[2][0] *= -1;

    a[2][1] = std::stof(m_entryY3.get_text());
    if(m_comboboxY3.get_active_text() == "-")
      a[2][1] *= -1;

    a[2][2] = std::stof(m_entryZ3.get_text());
    if(m_comboboxZ3.get_active_text() == "-")
      a[2][2] *= -1;

    b[0] = std::stof(m_entryB1.get_text());
    if(m_comboboxB1.get_active_text() == "-")
      b[0] *= -1;
  
    b[1] = std::stof(m_entryB2.get_text());
    if(m_comboboxB2.get_active_text() == "-")
      b[1] *= -1;
  
    b[2] = std::stof(m_entryB3.get_text()); 
    if(m_comboboxB2.get_active_text() == "-")
      b[2] *= -1; 

    Cramer cramer(a, b);   
    print("x = " + std::to_string(cramer.getX()) + " y = " + std::to_string(cramer.getY()) + " z = " + std::to_string(cramer.getZ()));   
  } else {
    print("Erro: dígito(s) inválido(s)"); 
  }
}

void Gui::set_hierarchy(){
  add(m_fixed);

  m_fixed.add(vbox); 
  vbox.pack_start(menuBar);
  
  menuFile.set_label("Arquivo");
  menuBar.append(menuFile);
  
  menuHelp.set_label("Ajuda");
  menuBar.append(menuHelp); 
  
  menuFile.set_submenu(subMenuFile);
  menuHelp.set_submenu(subMenuHelp);
  
  subMenuFile.append(menuFileQuit);
  subMenuHelp.append(menuHelpAbout); 
  
  menuFileQuit.set_label("Sair");
  menuHelpAbout.set_label("Sobre"); 
  
  menuFileQuit.signal_activate().connect(sigc::mem_fun(*this, &Window::hide));
  menuHelpAbout.signal_activate().connect(sigc::mem_fun(*this, &Gui::about));
    
  m_fixed.add(m_frame1);
  m_fixed.add(m_frame2);
  m_fixed.add(m_frame3);
  m_fixed.add(m_frame4);
  m_fixed.move(m_frame1, 2, 20);
  m_fixed.move(m_frame2, 2, 80);
  m_fixed.move(m_frame3, 2, 140);
  m_fixed.move(m_frame4, 317, 200); 
  
  m_frame1.add(m_alignment1);

  m_alignment1.add(m_box1); 

  m_box1.pack_start(m_comboboxX1);
  m_box1.pack_start(m_entryX1);
  m_box1.pack_start(m_labelX1);
  m_box1.pack_start(m_comboboxY1);
  m_box1.pack_start(m_entryY1);
  m_box1.pack_start(m_labelY1);
  m_box1.pack_start(m_comboboxZ1);
  m_box1.pack_start(m_entryZ1);
  m_box1.pack_start(m_labelZ1);
  m_box1.pack_start(m_comboboxB1);
  m_box1.pack_start(m_entryB1);

  m_frame2.add(m_alignment2);

  m_alignment2.add(m_box2);

  m_box2.pack_start(m_comboboxX2);
  m_box2.pack_start(m_entryX2);
  m_box2.pack_start(m_labelX2);
  m_box2.pack_start(m_comboboxY2);
  m_box2.pack_start(m_entryY2);
  m_box2.pack_start(m_labelY2);
  m_box2.pack_start(m_comboboxZ2);
  m_box2.pack_start(m_entryZ2);
  m_box2.pack_start(m_labelZ2);
  m_box2.pack_start(m_comboboxB2); 
  m_box2.pack_start(m_entryB2);

  m_frame3.add(m_alignment3);

  m_alignment3.add(m_box3);

  m_box3.pack_start(m_comboboxX3);
  m_box3.pack_start(m_entryX3);
  m_box3.pack_start(m_labelX3);
  m_box3.pack_start(m_comboboxY3);
  m_box3.pack_start(m_entryY3);
  m_box3.pack_start(m_labelY3);
  m_box3.pack_start(m_comboboxZ3);
  m_box3.pack_start(m_entryZ3);
  m_box3.pack_start(m_labelZ3);
  m_box3.pack_start(m_comboboxB3); 
  m_box3.pack_start(m_entryB3); 

  m_frame4.add(m_alignment4);

  m_alignment4.add(m_box4);

  m_box4.pack_start(m_button1);
  m_box4.pack_start(m_buttonClose);
}

void Gui::draw_widgets(){
  set_title("Regra de Cramer");
  //set_default_size(-5, -1);
  set_visible(true);
  set_can_focus(false);
  set_resizable(false);
  set_border_width( 6 );

  //m_fixed.set_size_request(/*1, 2*/-1, -1);
  m_fixed.set_visible(true);
  m_fixed.set_can_focus(false);

  // 1 

  //  m_alignment1.set_size_request(40, 6);
  m_alignment1.set_visible( true );
  m_alignment1.set_can_focus( true );
  m_alignment1.set_border_width(10);

  m_box1.set_visible( true );
  m_box1.set_can_focus( false );

  m_frame1.set_size_request(1, 1);
  m_frame1.set_visible( true );
  m_frame1.set_can_focus( false );
  m_frame1.set_margin_end( 8 );
  m_frame1.set_shadow_type( Gtk::ShadowType::SHADOW_IN );
  m_frame1.set_label_align( 0.02 );

  m_comboboxX1.append("+");
  m_comboboxX1.append("-");
  m_comboboxX1.set_active(0);
  
  m_comboboxY1.append("+");
  m_comboboxY1.append("-");
  m_comboboxY1.set_active(0);

  m_comboboxZ1.append("+");
  m_comboboxZ1.append("-");
  m_comboboxZ1.set_active(0);

  m_comboboxB1.append("+");
  m_comboboxB1.append("-");
  m_comboboxB1.set_active(0);

  m_entryX1.set_visible( true );
  m_entryX1.set_can_focus( true );
  m_entryX1.set_placeholder_text("x1");
  m_entryX1.set_max_length(4);
  m_entryX1.set_width_chars(4);
  
  m_entryY1.set_visible( true );
  m_entryY1.set_can_focus( true );
  m_entryY1.set_placeholder_text("y1");
  m_entryY1.set_max_length(4);
  m_entryY1.set_width_chars(4); 

  m_entryZ1.set_visible( true );
  m_entryZ1.set_can_focus( true );
  m_entryZ1.set_placeholder_text("z1");
  m_entryZ1.set_max_length(4);
  m_entryZ1.set_width_chars(4); 

  m_entryB1.set_visible(true);
  m_entryB1.set_can_focus(true);
  m_entryB1.set_placeholder_text("b1");
  m_entryB1.set_max_length(4);
  m_entryB1.set_width_chars(4); 

  m_labelX1.set_text("x  ");
  m_labelY1.set_text("y  ");
  m_labelZ1.set_text("z =  ");
  
  //

  // 2
  
  //m_alignment2.set_size_request(80, 12);
  m_alignment2.set_visible( true );
  m_alignment2.set_can_focus( true );
  m_alignment2.set_border_width(10);
  
  m_box2.set_visible( true );
  m_box2.set_can_focus( false );
  
  //m_frame2.set_size_request(/*10, 8*/5, 4);
  m_frame2.set_visible( true );
  m_frame2.set_can_focus( false );
  m_frame2.set_margin_end( 8 );
  m_frame2.set_shadow_type( Gtk::ShadowType::SHADOW_IN );
  m_frame2.set_label_align( 0.02 );

  m_comboboxX2.append("+");
  m_comboboxX2.append("-");
  m_comboboxX2.set_active(0);

  m_comboboxY2.append("+");
  m_comboboxY2.append("-");
  m_comboboxY2.set_active(0);

  m_comboboxZ2.append("+");
  m_comboboxZ2.append("-");
  m_comboboxZ2.set_active(0);

  m_comboboxB2.append("+");
  m_comboboxB2.append("-");
  m_comboboxB2.set_active(0);

  m_entryX2.set_visible( true );
  m_entryX2.set_can_focus( true );
  m_entryX2.set_placeholder_text("x2");
  m_entryX2.set_max_length(4);
  m_entryX2.set_width_chars(4); 
  
  m_entryY2.set_visible( true );
  m_entryY2.set_can_focus( true );
  m_entryY2.set_placeholder_text("y2");
  m_entryY2.set_max_length(4);
  m_entryY2.set_width_chars(4); 

  m_entryZ2.set_visible( true );
  m_entryZ2.set_can_focus( true );
  m_entryZ2.set_placeholder_text("z2");
  m_entryZ2.set_max_length(4);
  m_entryZ2.set_width_chars(4); 

  m_entryB2.set_visible(true);
  m_entryB2.set_can_focus(true);
  m_entryB2.set_placeholder_text("b2");
  m_entryB2.set_max_length(4);
  m_entryB2.set_width_chars(4); 

  
  m_labelX2.set_text("x  ");
  m_labelY2.set_text("y  ");
  m_labelZ2.set_text("z =  ");

  
  //

  // 3
  
  //m_frame3.set_size_request(/*20, 16*/5, 4);
  m_frame3.set_visible( true );
  m_frame3.set_can_focus( false );
  m_frame3.set_margin_end( 8 );
  m_frame3.set_shadow_type( Gtk::ShadowType::SHADOW_IN );
  m_frame3.set_label_align( 0.02 );
  
  m_box3.set_visible( true );
  m_box3.set_can_focus( false );

  //m_alignment3.set_size_request(160, 18);
  m_alignment3.set_visible( true );
  m_alignment3.set_can_focus( true );
  m_alignment3.set_border_width(10);
  
  m_comboboxX3.append("+");
  m_comboboxX3.append("-");
  m_comboboxX3.set_active(0);

  m_comboboxY3.append("+");
  m_comboboxY3.append("-");
  m_comboboxY3.set_active(0);

  m_comboboxZ3.append("+");
  m_comboboxZ3.append("-");
  m_comboboxZ3.set_active(0);
  
  m_comboboxB3.append("+");
  m_comboboxB3.append("-");
  m_comboboxB3.set_active(0); 
  
  m_entryX3.set_visible( true );
  m_entryX3.set_can_focus( true );
  m_entryX3.set_placeholder_text("x3");
  m_entryX3.set_max_length(4);
  m_entryX3.set_width_chars(4); 

  m_entryY3.set_visible( true );
  m_entryY3.set_can_focus( true );
  m_entryY3.set_placeholder_text("y3");
  m_entryY3.set_max_length(4);
  m_entryY3.set_width_chars(4); 
 
  m_entryZ3.set_visible( true );
  m_entryZ3.set_can_focus( true );
  m_entryZ3.set_placeholder_text("z3");
  m_entryZ3.set_max_length(4);
  m_entryZ3.set_width_chars(4); 
  
  m_entryB3.set_visible(true);
  m_entryB3.set_can_focus(true);
  m_entryB3.set_placeholder_text("b3");
  m_entryB3.set_max_length(4);
  m_entryB3.set_width_chars(4); 
  
  m_labelX3.set_text("x  ");
  m_labelY3.set_text("y  ");
  m_labelZ3.set_text("z =  ");

  //

  m_button1.set_label("Calcular");
  m_button1.set_visible(true);
  m_button1.set_can_focus(true);
  m_button1.set_focus_on_click(true);
  m_button1.set_border_width(5);
  m_button1.signal_clicked().connect(sigc::mem_fun(*this, &Gui::on_button1));

  m_buttonClose.set_label("Sair");
  m_buttonClose.set_visible(true);
  m_buttonClose.set_can_focus(true);
  m_buttonClose.set_focus_on_click(true);
  m_buttonClose.set_border_width(5);
  m_buttonClose.signal_clicked().connect(sigc::mem_fun(*this, &Window::hide));
  
}
